/**
*@file       pnUpdateService.cpp
*@brief      impl of net work service
*
*@author     graywind
*@version    1.0.0.0
*@data       2011/11/16
*/

#include "../include/pnupdateservice.h"
#include "boost/bind.hpp"
#include "boost/thread.hpp"

using namespace pnNetlib;


/***************************************************************************************************************************/
pnUpdateService::pnUpdateService(boost::asio::io_service& ioService, int update_thread_num):
m_ioService(ioService),
m_work(ioService),
m_iThreadNum(update_thread_num),
m_bIsStart(false)
{

}

/***************************************************************************************************************************/
void
pnUpdateService::vanish()
{
	for(ThreadList::iterator itr = m_listThreads.begin(); itr != m_listThreads.end(); ++itr)
	{
		(*itr)->join();
	}

	for(ThreadList::iterator itr = m_listThreads.begin(); itr != m_listThreads.end(); ++itr)
	{
		delete (*itr);
	}

	m_listThreads.clear();
}
/***************************************************************************************************************************/
pnUpdateService::~pnUpdateService()
{
	terminate();
}
/***************************************************************************************************************************/
void
pnUpdateService::start()
{
	if(m_bIsStart == false)
	{
		m_bIsStart = true;
		m_ioService.reset();

		for(int i = 0 ; i < m_iThreadNum; ++i)
		{
			m_listThreads.push_back(new boost::thread(boost::bind(&boost::asio::io_service::run, &m_ioService)));
		}
	}
}
/***************************************************************************************************************************/
void
pnUpdateService::terminate()
{
	if(m_bIsStart == true)
	{
		m_bIsStart = false;

		vanish();
	}
}
